<template xmlns="">
  <el-row :gutter="10">
   <el-col :span="3">
     <el-input v-model="name" placeholder="请输入姓名"></el-input>
   </el-col>
    <el-col :span="3">
     <el-button type="primary" @click="paging(1)">查询</el-button>
      <el-button type="warning" @click="add">添加用户</el-button>
   </el-col>
  </el-row>
  <el-table :data="page.list">
    <el-table-column label="序号" prop="id" ></el-table-column>
    <el-table-column label="姓名" prop="name"></el-table-column>
    <el-table-column label="年龄" prop="age"></el-table-column>
    <el-table-column label="操作">
      <template v-slot="scope">
        <el-button type="primary" @click="update(scope.row)">修改</el-button>
        <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="paging"
      :current-page="page.pageNum"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
  </el-pagination>

  <el-dialog title="用户" v-model="dialogFormVisible" :before-close="clear">
    <el-form :model="form" :rules="rules" ref="userForm" >
      <el-form-item label="姓名" :label-width="useFormLabelWidth" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="年龄" :label-width="useFormLabelWidth" prop="age">
        <el-input v-model.number="form.age"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="clear">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import {onMounted, ref} from 'vue';
import * as eslintPlugin from "@typescript-eslint/eslint-plugin";
import axios from "axios";
/*import { rules } from '@typescript-eslint/eslint-plugin';
import {useFormLabelWidth} from "element-plus/es/components/form/src/utils";*/
const  dialogFormVisible= ref(false)
const  form = ref({})
const  userForm = ref()
const  useFormLabelWidth = ref("100px")
const rules=ref(
    {
      name: [
        { required: true, message: '请输入姓名', trigger: 'blur' },
        { min: 1, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
      ],
      age: [
        { required: true, message: '请输入年龄', trigger: 'blur' },
        {type: 'number', min: 1 , max: 120, message: '年龄在 1 到 120 岁', trigger: 'blur' }
      ]
    }
)
const add = () => {
  dialogFormVisible.value = true
}
const clear = () => {
  form.value = {}
  dialogFormVisible.value = false
}
const save = () => {
  userForm.value.validate((valid: any) => {
    if(!valid){
    alert('验证未通过')
  }else {
      alert('验证通过')
      axios.post('user/save',form.value).then(res => {
        if (res.data) {
          alert('保存成功');
        } else {
          alert('保存失败');
        }
        clear()
        //查询列表
        paging(1)
      })
    }
  }
)}

//用户分页列表===================================================开始

const page=ref({})
const name = ref('')
const pageSize = ref(2)
const paging = ( curPage:any ) => {
  axios.get('user/list?curPage='+curPage+"&name="+name.value+"&pageSize="+pageSize.value).then(res => {
    page.value = res.data
  })
}

//用户选择下拉框的每页显示的条数后触发该函数
const handleSizeChange=(ps:any)=>{
  pageSize.value = ps
  paging(1)
}
//用户列表===================================================结束
onMounted(()=>{
  paging(1)
})

//修改===================================================开始
const update = (row:any) => {
  form.value = row
  dialogFormVisible.value = true
}
//修改===================================================结束
//删除===================================================开始
const del = (id:any) => {
  if (confirm("你确认删除吗?")){
    axios.post('user/del?id='+id).then(res => {
      if (res.data) {
        alert('删除成功')
      } else {
        alert('删除失败')
      }
      clear()
      //查询列表
      paging(1)
    })

  }
}
//删除===================================================结束
</script>

<style scoped>

</style>
